package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.myannotation.CacheKey;
import com.itheima.myannotation.Cacheable;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/checkItem")
public class CheckItemController {
    @Reference(version = "1.0.0")
    private CheckItemService checkItemService;

    @PostMapping("/findPage")
    public Result findPage( @RequestBody QueryPageBean queryPageBean) {
        if (queryPageBean.getQueryString()!=null && queryPageBean.getCurrentPage() != 1) {
            if(queryPageBean.getQueryString().equals("")){
                queryPageBean.setCurrentPage(queryPageBean.getCurrentPage());
            }else{
                queryPageBean.setCurrentPage(1);
            }

        }
        return checkItemService.findPAge(queryPageBean);
    }

    @PostMapping("/add")
    public Result add(@RequestBody CheckItem checkItem) {
        long count01 = checkItemService.findByName(checkItem.getName());
        long count02 = checkItemService.findByCode(checkItem.getCode());
        if (count01 != 0) {
            return Result.fail(-999, "已有该项目，添加失败");
        }
        if (count02 != 0) {
            return Result.fail(-999, "已有该编号，添加失败");
        }
        return checkItemService.add(checkItem);
    }

    @PostMapping("/update")
    public Result update(@RequestBody CheckItem checkItem) {
        return checkItemService.update(checkItem);
    }

    @PreAuthorize("hasAnyAuthority('CHECKITEM_DELETE')")
    @GetMapping("/delete")
    public Result delete(Integer id) {
        System.out.println(1/0);
        return checkItemService.delete(id);
    }


}
